<?php
class inman_ctl_admin_stores extends desktop_controller{
	public function __construct($app){
		//$this->app=app::get('inman');
		parent::__construct($app);
		$this->checkObject=kernel::single('inman_store_object');
		$this->model=kernel::single('inman_mdl_stores');
	}
	public function index(){
		$this->finder('inman_mdl_stores',array(
			'title'=>app::get('inman')->_('店铺列表'),
			'actions'=>array(
				array(
						'label'=>app::get('inman')->_('添加店铺'),
						'href'=>'index.php?app=inman&ctl=admin_stores&act=add',
						'target'=>'dialog::{title:\''.app::get('inman')->_('添加店铺').'\',width:800,height:570,}',
						),
				),
	        'use_buildin_tagedit'=>true,//标签管理
	        'use_buildin_set_tag'=>true,
	        'use_buildin_export'=>true,//导出
	        'use_buildin_import'=>true,//导入
	        'use_buildin_recycle'=>true,//删除
	        //'use_view_tab'=>false,
	        'allow_detail_popup'=>true,
			));
	}
	//添加的店铺页面
	public function add(){
		//生成店铺码、登录的分销商
		$storeBn=$this->checkObject->gen_store_bn();
		$login_account=$this->checkObject->get_login_account();
		$this->pagedata['login_account']=$login_account;
		$this->pagedata['storeBn']=$storeBn;
		$this->page('admin/stores/add.html');
	}
	//添加店铺的操作
	public function toAdd(){
		$this->begin();
		//组装数据
		$store=$_POST['store'];
		//echo '<pre>',print_r($store),'</pre>';die;
		//验证数据(验证店铺的名称和经纬度的范围)
		if(!$this->checkObject->checkName($store['store_name'],$msg)){
			$this->end(false,$msg);
		}
		if(!$this->checkObject->checkCoordinates($store['store_x'],$store['store_y'],$msg)){
			$this->end(false,$msg);
		}
		//检查身份证
		//检查银行卡
		if(preg_match('/^(\d{16}|\d{19})$/',$store['bank_card_number'])){
			$this->end(false,app::get('inman')->_('银行卡格式不正确'));
		}
		//...其他的检测
		$store['store_add_time']=$store['store_update_time']=time();
		$this->model->save($store);

		//将对应账户添加到ec的会员系统中
        $userPassport = kernel::single('b2c_user_passport');
        $process_data['login_name'] = $store['login_account'];
        $process_data['login_password'] = '888888';
        $saveData['b2c_members']['source'] = 'api';
        $pamAccount = $userPassport->pre_account_signup_process($process_data);
        $saveData['pam_account'] = $pamAccount;
        if( $member_id = $userPassport->save_members($saveData,$msg) ) {
            foreach (kernel::servicelist('b2c_save_post_om') as $object) {
                $object->set_arr($member_id, 'member');
                $refer_url = $object->get_arr($member_id, 'member');
            }
            /*注册完成后做某些操作! begin*/
            foreach (kernel::servicelist('b2c_register_after') as $object) {
                $object->registerActive($member_id);
            }
            //增加会员同步 2012-5-15
            if ($member_rpc_object = kernel::service("b2c_member_rpc_sync")) {
                $member_rpc_object->createActive($member_id);
            }
            /*end*/
            $this->end(true,app::get('inman')->_('店铺添加成功'));
        }

	}
	//编辑店铺的页面
	public function editStore(){
		//echo '<pre>',print_r($_POST['store']),'</pre>';
		if(!empty($_POST['store'])){
			$store=$_POST['store'];
			$store['store_update_time']=time();
			$this->begin('index.php?app=inman&ctl=admin_stores&act=index');
				if(!$this->checkObject->checkName($store['store_name'],$msg,$store['store_id'])){
					$this->end(false,$msg);
				}
				if(!$this->checkObject->checkCoordinates($store['store_x'],$store['store_y'],$msg)){
					$this->end(false,$msg);
				}
				$this->model->save($store);
			$this->end(true,'店铺编辑成功');
		}
	}
	//编辑绑定的分销商信息
	public function editUser(){
		if(!empty($_POST['store'])){
			$store=$_POST['store'];
			$store['store_update_time']=time();
			$this->begin('index.php?app=inman&ctl=admin_stores&act=index');
			//验证银行卡、和联系方式 的格式
			$this->model->save($store);
			$this->end(true,'店铺编辑成功');
		}
	}
	//ajax验证用户名
	public function checkname(){
		if(!$this->checkObject->checkName($_POST['store']['store_name'],$msg)){
			echo json_encode(array('error'=>$msg));exit;
		}
		echo json_encode(array('success'=>app::get('inman')->_('店铺名称可用')));exit;
	}
	//ajax验证经纬度
	public function checkCoordinates(){
		if(!$this->checkObject->checkCoordinates($_POST['store']['store_coordinates'],$msg)){
			echo json_encode(array('error'=>$msg));exit;
		}
		echo json_encode(array('success'=>app::get('inman')->_('经纬度格式正确')));exit;
	}
	//后台finer区域
	public function _views(){
		$return=array(
				1=>array('label'=>app::get('inman')->_('所有'),'optional'=>false,'filter'=>array()),
				2=>array('label'=>app::get('inman')->_('启用'),'optional'=>false,'filter'=>array('status'=>'1')),
				3=>array('label'=>app::get('inman')->_('禁用'),'optional'=>false,'filter'=>array('status'=>'0')),
		);
		return $return;
	}
}